﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EdexApiClient
{
    public partial class EdexApiContext
    {
        /// <summary>
        /// Получить перечень складов
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_LOCATIONS> GetLocations()
        {
            return this.GetLocations(String.Empty);
        }

        /// <summary>
        /// Получить перечень складов
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_LOCATIONS> GetLocations(string search)
        {
            var uri = String.Format("LOCATIONS/?api_key={0}", this.session.APIKEY);

            if (!String.IsNullOrEmpty(search))
                uri += "&$filter=substringof('" + search + "', LOCATION_NAME) eq true&$top=100";

            uri += "&$orderby=LOCATION_NAME";

            return this.GetData<List<WH_LOCATIONS>>(uri);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WH_LOCATIONS GetLocationDetails(Guid id)
        {
            var uri = String.Format("LOCATIONS/{0}/?api_key={1}", id.ToString(), this.session.APIKEY);
            return this.GetData<WH_LOCATIONS>(uri);
        }

        public LOCATION_MAP GetLocationMap(string id)
        {
            var uri = String.Format("LOCATIONS_MAPPING/?LOCATION_GUID={0}&api_key={1}", id, this.session.APIKEY);
            return this.GetData<LOCATION_MAP>(uri);
        }

        public void SaveLocationMap(Guid LEFT_LOCATION_GUID, Guid RIGHT_LOCATION_GUID)
        {
            this.Post<LOCATION_MAP>(String.Format("LOCATIONS_MAPPING/?api_key={0}", this.session.APIKEY),
                new LOCATION_MAP
                {
                    LEFT_LOCATION_MAP = new WH_LOCATIONS { GUID_RECORD = LEFT_LOCATION_GUID },
                    RIGHT_LOCATION_MAP = new WH_LOCATIONS { GUID_RECORD = RIGHT_LOCATION_GUID }
                });
        }

        public void AddLocation(WH_LOCATIONS data)
        {
            var uri = String.Format("LOCATIONS/?api_key={0}", this.session.APIKEY);
            this.Post<WH_LOCATIONS>(uri, data);
        }

        public void UpdateLocation(WH_LOCATIONS data)
        {
            var uri = String.Format("LOCATIONS/?api_key={0}", this.session.APIKEY);
            this.Put<WH_LOCATIONS>(uri, data);
        }

        public void DeleteLocation(string id)
        {
            var uri = String.Format("LOCATIONS/{0}/?api_key={1}", id, this.session.APIKEY);
            this.Delete(uri);
        }
    }
}